package ourfilter;


import java.awt.image.renderable.ParameterBlock;

import javafx.scene.transform.MatrixType;

import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.PlanarImage;

import ourImage.OurPlanarImage;
import pimpmypipe.filter.DataTransformationFilter;
import pimpmypipe.interfaces.Readable;
import pimpmypipe.interfaces.Writeable;
import pimpmypipe.pipe.Pipe;

/**
 * Opening Filter: Including erode and dilate
 * 
 * @author Meraner Marcel
 */
public class DilateFilter extends DataTransformationFilter<OurPlanarImage> {

    // Dilate Matrix
    private float[] dilate = new float[]{
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 1, 1, 1, 0, 0, 0,
        0, 0, 1, 1, 1, 1, 1, 0, 0,
        0, 1, 1, 1, 1, 1, 1, 1, 0,
        0, 1, 1, 1, 1, 1, 1, 1, 0,
        0, 1, 1, 1, 1, 1, 1, 1, 0,
        0, 0, 1, 1, 1, 1, 1, 0, 0,
        0, 0, 0, 1, 1, 1, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0
    };
    
    private int _matrixSize = 9; 
    private KernelJAI kernelDilate = null;
    private boolean _filterEnabled = false;
    private float _matrixValue = 1.0f;

    public DilateFilter(Pipe<OurPlanarImage> in, Pipe<OurPlanarImage> out) {
        super(in, out);
        in.setOut(this);
        out.setIn(this);
        // Generate Kernels
        kernelDilate = new KernelJAI(_matrixSize, _matrixSize, dilate);
    }
    public DilateFilter(Readable<OurPlanarImage> in, Writeable<OurPlanarImage> out){
        super(in, out);
        kernelDilate = new KernelJAI(_matrixSize, _matrixSize, dilate);
    }

    @Override
    protected void process(OurPlanarImage entity) {
        if (_filterEnabled) {
            // Dilate the given image
            ParameterBlock pbDilate = new ParameterBlock();
            pbDilate.addSource(entity.getImage());
            pbDilate.add(kernelDilate);
            PlanarImage image = JAI.create("dilate", pbDilate);
            entity.setImage(image);
        }
    }

    public void setMatrixSize(int matrixSize) {
        _matrixSize = matrixSize;
        kernelDilate = new KernelJAI(_matrixSize, _matrixSize, dilate);
    }

    public boolean isEnabled() {
        return _filterEnabled;
    }

    public void setFilterEnabled(boolean enabled) {
        _filterEnabled = enabled;
    }

    public void setMatrixValue(float matrixValue) {
        _matrixValue = matrixValue;
        kernelDilate = new KernelJAI(_matrixSize, _matrixSize, dilate);
    }
    
    
}
